package com.gitee.linzl.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.gitee.linzl.domain.User;
import com.gitee.linzl.repository.UserRepository;

@Service
public class UserServiceImpl implements UserService {
	private final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Autowired
	private UserRepository userRepository;

	@Override
	public void createUser(User user) {
		User existing = userRepository.findByUsername(user.getUsername());
		Assert.isNull(existing, "user already exists: " + user.getUsername());

		String hash = passwordEncoder.encode(user.getPassword());
		user.setPassword(hash);
		userRepository.save(user);
		log.info("new user has been created: {}", user.getUsername());
	}
}
